package com.sunshine.lnuplus.utils

import java.lang.StringBuilder
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

object DateUtils{
    private val calendar = Calendar.getInstance()

    val year = calendar[Calendar.YEAR]

    val month = calendar[Calendar.MONTH] + 1

    val day = calendar[Calendar.DAY_OF_MONTH]

    val dayOfWeek = calendar[Calendar.DAY_OF_WEEK]

    val weekOfYear = calendar[Calendar.WEEK_OF_YEAR]

    val hour = calendar[Calendar.HOUR_OF_DAY]

    val minute = calendar[Calendar.MINUTE]

    val second = calendar[Calendar.SECOND]

    val ms = calendar[Calendar.MILLISECOND]

    fun getThisWeek(month: Int, day: Int): Int {
        val startCalendar = Calendar.getInstance()
        var startWeek: Int
        val thisWeek: Int
        //上学期
        return if (month > 8) {
            startCalendar[Calendar.MONTH] = month
            startCalendar[Calendar.DAY_OF_MONTH] = day
            startWeek = startCalendar[Calendar.WEEK_OF_YEAR]
            //开学那天为周日
            if (startCalendar[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
                startWeek--
            }
            //今天为周日
            if (dayOfWeek == 1) {
                startWeek++
            }
            thisWeek = weekOfYear - startWeek + 1
            //上课周
            if (thisWeek > 0) {
                thisWeek
            } //放假
            else {
                1
            }
        } else {
            startCalendar[Calendar.MONTH] = month
            startCalendar[Calendar.DAY_OF_MONTH] = day
            startWeek = startCalendar[Calendar.WEEK_OF_YEAR]
            if (startCalendar[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
                startWeek--
            }
            if (dayOfWeek == 1) {
                startWeek++
            }
            thisWeek = weekOfYear - startWeek + 1
            //上课周
            if (thisWeek > 0) {
                thisWeek
            } //放假
            else {
                1
            }
        }
    }

    fun getTodayOfWeek() : Int{
        return if(dayOfWeek == 1) 7 else dayOfWeek - 1
    }

    fun getTodayOfWeekString(day: Int) : String{
        return when(day){
            1 -> "一"
            2 -> "二"
            3 -> "三"
            4 -> "四"
            5 -> "五"
            6 -> "六"
            7 -> "日"
            else -> ""
        }
    }

    fun getTodayOfWeekStringToInt(day: String) : Int{
        return when(day.replace("周", "星期")){
            "星期一" -> 1
            "星期二" -> 2
            "星期三" -> 3
            "星期四" -> 4
            "星期五" -> 5
            "星期六" -> 6
            "星期日" -> 7
            else -> 1
        }
    }

    fun getJieToStart(jie: String) : Int{
        return when(jie){
            "第一大节" -> 1
            "第二大节" -> 2
            "第三大节" -> 3
            "第四大节" -> 4
            "第五大节" -> 5
            else -> 1
        }
    }

    fun getWeeks(weeks: String) : String{
        return when(weeks){
            "全周上课" -> "1-16"
            "单周上课" -> "1,3,5,7,9,11,13,15"
            "双周上课" -> "2,4,6,8,10,12,14,16"
            else -> weeks
        }
    }

    fun getCountDownTime(time: String): Int {
        if (time == "") return 0
        val simpleFormat = SimpleDateFormat("yyyy-MM-dd hh:mm")
        val today = "$year-$month-$day $hour:$minute"
        /*天数差*/
        var fromDate1: Date? = null
        var toDate1: Date? = null
        try {
            fromDate1 = simpleFormat.parse(today)
            toDate1 = simpleFormat.parse(time)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        val from1 = fromDate1!!.time
        val to1 = toDate1!!.time
        return ((to1 - from1) / (1000 * 60 * 60 * 24)).toInt()
    }

    fun getZFWeeks(time: String) : String{
        var weeks = ""
        val temp = time.replace("周(.*)\\{".toRegex(), "").replace("}", "")
        when {
            temp.contains("双周") -> {
                val ttemp = temp.substring(1, temp.length).split("|")[0].split("-")
                for(i in ttemp[0].toInt()..ttemp[1].substring(0, ttemp[1].length - 1).toInt()){
                    if(i%2 ==0){
                        weeks += "$i,"
                    }
                }
                weeks = weeks.substring(0, weeks.length - 1)
            }
            temp.contains("单周") -> {
                val ttemp = temp.substring(1, temp.length).split("|")[0].split("-")
                for(i in ttemp[0].toInt()..ttemp[1].substring(0, ttemp[1].length - 1).toInt()){
                    if(i%2 !=0){
                        weeks += "$i,"
                    }
                }
                weeks = weeks.substring(0, weeks.length - 1)
            }
            else -> {
                weeks = temp.substring(1, temp.length - 1)
            }
        }
        return weeks
    }

    fun getQZTimeStart(s: String): Int {
        val temp = s.replace("${s[0]}(.*)\\[".toRegex(), "").replace("]节", "")
        return temp.split("-")[0].toInt()
    }

    fun getQZTimeEnd(s: String): Int {
        val temp = s.replace("${s[0]}(.*)\\[".toRegex(), "").replace("]节", "")
        val ttemp = temp.split("-")
        return ttemp[ttemp.size - 1].toInt()
    }

    fun getQZWeeks(weeksHtml: String): String{
        var weeks = ""
        val temp = weeksHtml.replace("(周)", "")
        val weekList = temp.split(",")
        weekList.forEach {
            val be = it.split("-")
            if(be.size == 1){
                weeks += "${be[0]},"
            }else{
                when {
                    it.contains("单") -> {
                        for (i in be[0].toInt()..be[1].replace("(单周)", "").toInt()){
                            if(i % 2 != 0) weeks += "$i,"
                        }
                    }
                    it.contains("双") -> {
                        for (i in be[0].toInt()..be[1].replace("(双周)", "").toInt()){
                            if(i % 2 == 0) weeks += "$i,"
                        }
                    }
                    else -> {
                        for (i in be[0].toInt()..be[1].toInt()){
                            weeks += "$i,"
                        }
                    }
                }
            }
        }
        return weeks.substring(0, weeks.length - 1)
    }

    fun getXQZWeeks(s: String): String{
        val temp = s.replace("\\[(.*)节".toRegex(), "")
        val ttemp = temp.replace("\\((.*)\\)".toRegex(), "").split("-")
        when {
            temp.contains("单周") -> {
                return if(ttemp.size == 1) ttemp[0]
                else{
                    var tttemp = ""
                    for(i in ttemp[0].toInt()..ttemp[1].toInt()){
                        if(i % 2 != 0) tttemp += "$i,"
                    }
                    tttemp.substring(0, tttemp.length - 1)
                }
            }
            temp.contains("双周") -> {
                return if(ttemp.size == 1) ttemp[0]
                else{
                    var tttemp = ""
                    for(i in ttemp[0].toInt()..ttemp[1].toInt()){
                        if(i % 2 == 0) tttemp += "$i,"
                    }
                    tttemp.substring(0, tttemp.length - 1)
                }
            }
            else -> {
                return temp.replace("\\((.*)\\)".toRegex(), "")
            }
        }
    }

    fun getQGWeeks(s: String) : String{
        val temp = s.replace("周", "")
        var weeks = ""
        temp.split(",").forEach {
            if(it.split("-").size < 2){
                weeks += "$it,"
            }else{
                for(i in it.split("-")[0].toInt()..it.split("-")[1].toInt()){
                    weeks += "$i,"
                }
            }
        }
        return weeks.substring(0, weeks.length - 1)
    }

    fun getZNMZTime(time: String): List<Int> {
        val timeTemp = time.replace("\\(第(.*)节\\)".toRegex(), "")
        val timeString = time.replace(timeTemp, "").replace("(第", "").replace("节)", "")
        val timeList = ArrayList<Int>()
        timeList.add(timeString.split("-")[0].toInt())
        timeList.add(timeString.split("-")[1].toInt())
        return timeList
    }

    fun getZNMZWeeks(weeks: String): String {
        var resloveWeeks = weeks.replace("周", "")
        if(resloveWeeks.contains("双")){
            val tempWeeks = resloveWeeks.replace("(双)", "")
            resloveWeeks = ""
            for (i in tempWeeks.split("-")[0].toInt()..tempWeeks.split("-")[1].toInt()){
                if(i % 2 == 0){
                    resloveWeeks += "$i,"
                }
            }
            resloveWeeks = resloveWeeks.substring(0, resloveWeeks.length - 1)
        }else if(resloveWeeks.contains("单")){
            val tempWeeks = resloveWeeks.replace("(单)", "")
            resloveWeeks = ""
            for (i in tempWeeks.split("-")[0].toInt()..tempWeeks.split("-")[1].toInt()){
                if(i % 2 != 0){
                    resloveWeeks += "$i,"
                }
            }
            resloveWeeks = resloveWeeks.substring(0, resloveWeeks.length - 1)
        }
        return resloveWeeks
    }

    fun resloveNJSJJSTime(time: String) : List<List<String>>?{
        val lessonList = ArrayList<List<String>>()
        val param = time.split(" ")
        if(param.size % 6 != 0) return null
        for (i in 0 until param.size / 6){
            val list = ArrayList<String>()
            list.add(param[0 + 6 * i].replace("上课地点：", ""))
            var weeks = ""
            val weekList = param[1 + 6 * i].replace("上课时间：第", "").replace("上课时间：", "").replace("周", "").split(",")
            weekList.forEach {
                when{
                    it.contains("双") -> {
                        val se = it.replace("之双", "").split("-")
                        if(se.size == 1){
                            weeks += "${se[0]},"
                        }else{
                            for(i in se[0].toInt()..se[1].toInt()){
                                if(i % 2 == 0){
                                    weeks += "$i,"
                                }
                            }
                        }
                    }
                    it.contains("单") -> {
                        val se = it.replace("之单", "").split("-")
                        if(se.size == 1){
                            weeks += "${se[0]},"
                        }else{
                            for(i in se[0].toInt()..se[1].toInt()){
                                if(i % 2 != 0){
                                    weeks += "$i,"
                                }
                            }
                        }
                    }
                    else -> {
                        val se = it.split("-")
                        if(se.size == 1){
                            weeks += "${se[0]},"
                        }else{
                            for(i in se[0].toInt()..se[1].toInt()){
                                weeks += "$i,"
                            }
                        }
                    }
                }
            }
            list.add(weeks.subSequence(0, weeks.length - 1).toString())
            list.add(param[3 + 6 * i])
            val tse = param[5 + 6 * i].replace("节", "").split("-")
            list.add(tse[0])
            list.add(tse[1])
            lessonList.add(list)
        }
        return lessonList
    }

    fun getXZFWeeks(s: String): String {
        var weeks = ""
        val weekList = s.replace("周数：", "").replace("周", "").split(",")
        weekList.forEach {
            val weekItem = it.split("-")
            if(weekItem.size == 1){
                weeks += "${weekItem[0]},"
            }else{
                when {
                    it.contains("双") -> {
                        for (i in weekItem[0].toInt()..weekItem[1].replace("(双)", "").toInt()){
                            if(i % 2 == 0) weeks += "$i,"
                        }
                    }
                    it.contains("单") -> {
                        for (i in weekItem[0].toInt()..weekItem[1].replace("(单)", "").toInt()){
                            if(i % 2 != 0) weeks += "$i,"
                        }
                    }
                    else -> {
                        for (i in weekItem[0].toInt()..weekItem[1].toInt()){
                            weeks += "$i,"
                        }
                    }
                }
            }
        }
        return weeks.substring(0, weeks.length - 1)
    }
}